//
//  BVHomeSceneHeaderView.m
//  AFNetworking
//
//  Created by 崔晋 on 2019/8/11.
//

#import "BVHomeSceneHeaderView.h"
#import "BVHomeHeaderCollectionViewCell.h"
#import "Masonry.h"

@interface BVHomeSceneHeaderView ()<UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topMargin;

@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (strong, nonatomic) UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *indictorWidth;
@property (weak, nonatomic) IBOutlet UIView *shadowView;


@end

@implementation BVHomeSceneHeaderView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.topMargin.constant = kStatusBarHeight;
    [self.containerView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.containerView).insets(UIEdgeInsetsMake(0, 0, 0, 54));
    }];
    [self.containerView sendSubviewToBack:self.collectionView];
    
    
    
    self.shadowView.backgroundColor = [UIColor clearColor];
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.frame = self.shadowView.bounds;
    layer.colors =
  @[(__bridge id)[UIColor colorWithWhite:1 alpha:0.5].CGColor,
    (__bridge id)[UIColor colorWithWhite:1 alpha:0.7].CGColor,
    (__bridge id)[UIColor colorWithWhite:1 alpha:0.9].CGColor,
    (__bridge id)[UIColor colorWithWhite:1 alpha:1].CGColor,
    (__bridge id)[UIColor colorWithWhite:1 alpha:1].CGColor];
    
    layer.startPoint = CGPointMake(0, 0.5);
    layer.endPoint = CGPointMake(1, 0.5);
    
    layer.locations = @[@0.2,@0.4,@0.6,@0.8];
    [self.shadowView.layer addSublayer:layer];
    [self.containerView bringSubviewToFront:self.shadowView];
    
}
- (IBAction)workoutDetailAct:(id)sender {
    self.workoutDetailBlock?self.workoutDetailBlock():nil;
}

- (void)setDataSource:(NSArray<BVWorkoutModel *> *)dataSource {
    _dataSource = dataSource;
    [self.collectionView reloadData];
}

- (void)setPage:(NSInteger)page {
    _page = page;
    [self.collectionView setContentOffset:[self targetPointWithPage:page] animated:NO];
    [self.collectionView reloadData];
}

#pragma mark - collectionView delegate and datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BVHomeHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BVHomeHeaderCollectionViewCell" forIndexPath:indexPath];
    [cell configWithModel:self.dataSource[indexPath.row] type:(self.page == indexPath.row)];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (self.dataSource.count > 0) {
        self.selectWorkOut?self.selectWorkOut(self.dataSource[indexPath.row]):nil;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [BVHomeHeaderCollectionViewCell itemSizeWithType:(indexPath.row == self.page)];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(45, 27);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(kSCREEN_WIDTH - 54 - (45 + 60), 27);
}

- (CGPoint)targetPointWithPage:(NSInteger)page {
    CGPoint targetContentOffset;
    targetContentOffset = CGPointMake(page * 13 + (8 * page), 0);
    return targetContentOffset;
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                     withVelocity:(CGPoint)velocity
              targetContentOffset:(inout CGPoint *)targetContentOffset {
    if (self.collectionView == scrollView) {
        CGFloat contentOffsetX = scrollView.contentOffset.x;
        NSInteger page = contentOffsetX / 21;
        if (self.page * 21 < contentOffsetX) {
            page = (contentOffsetX - 60) / 21 + 1;
        }
        if (velocity.x > 0) {
            page++;
        }else if (page < 0){
            page--;
        }
//        *targetContentOffset = [self targetPointWithPage:page];
        
        if (page > self.dataSource.count - 1) {
            page = self.dataSource.count - 1;
        }
        if (page < 0) {
            page = 0;
        }
        self.page = page;
        if (self.dataSource.count > 0) {
            self.selectWorkOut?self.selectWorkOut(self.dataSource[page]):nil;
        }
        [self.collectionView reloadData];
    }
}



- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = 8;
        flowLayout.minimumInteritemSpacing = 8;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerNib:[UINib nibWithNibName:@"BVHomeHeaderCollectionViewCell" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_HOME_SCENE_MODULE]] forCellWithReuseIdentifier:@"BVHomeHeaderCollectionViewCell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.scrollEnabled = NO;
    }
    return _collectionView;
}

@end
